import Vue 				from 'vue'
import util 			from '../../util'
import axios 			from 'axios'
import { List,Toast } 	from 'vant'
Vue.use(List,Toast)

export default {
	name: 'orderList',
	data(){
		return {
			pageState:1,		//1:好友管理   2:子代理管理
			pageIndex:1,		//页数
			list:[],
			loading:false,
			finished:false,
			loadError:false,
			offset:200,
			seeDetail:false,		//是否允许查看详细
			keyWords:'',	    //搜索关键词
			timer:null,
			distance:0,
			user:0,
			pageSize:20,			//每页数量
		}
	},
	watch:{
		keyWords(newVal,oldVal){
			if(newVal && newVal != oldVal){
				clearTimeout(this.timer);
				this.timer = setTimeout(() => {
					this.pageIndex = 1;
					this.list = [];
					this.finished = false;
					this.loading = true;
					this.load();
				},200)
			}else {
				this.pageIndex = 1;
				this.list = [];
				this.finished = false;
				this.loading = true;
				this.load();
			}
		}
	},
	methods:{
		jump(account,uid){
			if(this.locale !='zh') return
			if(this.seeDetail){
				this.$router.push({
					path:'/inTrade',
					query:{
						account:account,
						uid:uid
					}
				})
			}
		},
		load(){
			this.loading = true;
			let self = this;
			setTimeout(()=>{
				let _url = self.pageState == 2?'/partner/agent-list':'/partner/user-list';
				let params = {
					page:self.pageIndex,
					key:self.keyWords,
					size:self.pageSize
				}
				let md5Key = util.getHashStr(params)
                params['sign'] = md5Key;
				util.fetch(util.ajaxUrl+_url,params).then(res => {
					self.loading = false;
					if(res.retCode === 0){
						self.pageIndex++
						self.loadError = false;
						//判断是否允许查看详细
						if(res.retData && res.retData.user && res.retData.user.is_show_child_partner_detail && res.retData.user.is_show_child_partner_detail==='1'){
							self.seeDetail = true;
						}
						let list = res.retData.list || res.retData.lists;
						list.map(item => {
							item.showTime = '';
							if(item.create_time) {
								let _time = util.transTime4(item.create_time);
								item.showTime = _time[1]+'/'+ _time[2] +' '+ _time[3] +':'+_time[4]
							}
						})
						if(!self.keyWords){
							self.list = self.list.concat(list);
						}else{
							//搜索结果
							self.list = list;
						}
						if(!res.retData || list && list.length < 10){
							self.finished = true;
						}
					}else{
						self.loadError = true;
						Toast(res.retMsg || self.$t('noError'))
					}
				}).catch(error => {
					self.loading = false;
					self.loadError = true;
					console.log(error)
				})
			},500)
		},
		changeState(state){
			//改变tab
			if(this.loading) return
			this.pageIndex = 1;
			this.list = [];
			this.pageState = state;
			this.finished = false;
			this.loading = true;
			this.load();
			if(state === 2){
				this.$nextTick(() => {
            		if(this.$refs && this.$refs.scroll){
						this.$refs.scroll.onscroll = res => {
							this.distance = res.target.scrollLeft;
						}
					}
        		})
			}
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		document.querySelector('body').setAttribute('style','background:#fff');
		if(this.$route.query.tab) this.pageState = this.$route.query.tab;
		if(this.$route.query.user) this.user = this.$route.query.user;
		util.waitSDKReady().then(token => {
			this.token = token;
		}).catch((e) => {
			console.log('获取token失败')
		})
		setTimeout(()=>{
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				util.callHandler('setStatusBarStyle',{
					style:'dark'
				})
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}